import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IGroupMoneyOption } from "./NormalGroupMoneyStrategy";

/**
 * 分组策略
 */
export class WisdomGroupMoneyStrategy implements IMoneyStrategy {
    option: IGroupMoneyOption;
    strategyName: string = "";
    debugCompoenent: string = "WisdomGroupMoneyStrategy";
    groupResultArray: number[] = [];
    levelArray: number[] = [];
    currentRecmMoney: number = 0;
    allResultArray: number[] = [];

    constructor(name?: string, option?: IGroupMoneyOption) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                groupCount: 12,
                addStep: 100,
                devideStep: 100,
                maxLevel:3000,
                reverse: false //true:赢了+100，输了-100
            }
        }
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm(): void {
    }
    copyValue(valueObj: any): void {
        let obj = valueObj as WisdomGroupMoneyStrategy
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            Object.assign(this.option, obj.option);
            this.groupResultArray = obj.groupResultArray;
            this.levelArray = obj.levelArray;
            this.allResultArray = obj.allResultArray;
            this.currentRecmMoney = obj.currentRecmMoney;
        }
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = userRecm.recmV;
        let recmMoney = this.currentRecmMoney < baseMoney ? baseMoney : this.currentRecmMoney;

        if ((recmInfo.betCount) % this.option.groupCount == 0 && recmInfo.betCount > 0 && recmInfo.betCount != recmInfo.preBetCount) {
            let prev = 0;
            if (this.levelArray.length > 0) {
                prev = Math.abs(this.levelArray[this.levelArray.length - 1]);
            }

            let diff = recmInfo.roundSummaryInfo.winCount - recmInfo.roundSummaryInfo.failCount;
            this.groupResultArray.push(diff);
            this.allResultArray.push(diff);
            let csum = this.groupResultArray.reduce((prev, curr) => prev + curr, 0);

            if (diff > 0 && csum >= prev) {
                while (csum >= prev && this.levelArray.length > 0) {
                    if (prev > 0) {
                        this.groupResultArray = [];
                        this.levelArray.pop();
                    }
                    csum = csum - prev;
                    prev = Math.abs(this.levelArray[this.levelArray.length - 1]);
                }
                if (csum > 0) {
                    this.levelArray[this.levelArray.length - 1] += csum;
                }
            }
            else if (diff > 0 && csum < 0) {
                let step = this.option.addStep;
                recmMoney += step;
                this.groupResultArray = [];
                this.levelArray.push(csum);
            }
            else {
            }

            recmInfo.resetRoundSummaryInfo();
            userRecm.resetRoundSummaryInfo();
        }
        if (recmMoney < baseMoney) {
            recmMoney = baseMoney;
        }
        this.currentRecmMoney = recmMoney;
        return [recmV, recmMoney];
    }
    resetStrategy() {
        this.groupResultArray = [];
        this.levelArray = [];
        this.allResultArray = [];
        this.currentRecmMoney = 0;
    }
}